package com.dachuang.canjiren.service.impl;

import com.dachuang.canjiren.entity.*;
import com.dachuang.canjiren.entity.VO.CommentVO;
import com.dachuang.canjiren.mapper.*;
import com.dachuang.canjiren.service.FollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dachuang.common.exception.BizCodeEnume;
import com.dachuang.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-07-27
 */
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements FollowService {

    @Autowired
    private FollowMapper followMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private BusinessMapper businessMapper;
    @Override
    public R insertFollow(Integer userId, Integer followId) {
        if (Objects.isNull(userId)){
            return R.error(BizCodeEnume.ERROR20006);
        }
        if (StringUtils.isEmpty(followId)) {
            return R.error(BizCodeEnume.ERROR40001);
        }

        //验证userId
        User user = userMapper.selectById(userId);
        if (Objects.isNull(user)){
            return R.error(BizCodeEnume.ERROR20004);
        }
        //验证business
        Business business = businessMapper.selectBusiness(followId);
        if (Objects.isNull(business)){
            return R.error(BizCodeEnume.ERROR40001);
        }
        //插入到表
        Follow follow = new Follow();
        follow.setUserId(userId);
        follow.setFollowId(followId);
        if (followMapper.selectFollow(follow) == null) {
            if (followMapper.insertFollow(follow) > 0) {
                return R.ok();
            }
        }else{
            if (followMapper.deleteFollow(follow) > 0) {
                return R.ok();
            }
        }
        return R.error();

    }

    @Override
    public R selectFollowList() {
        List<Follow> follow = followMapper.selectFollowList();
        R r = R.ok();
        r.put("data",follow);
        return r;
    }


}
